<template>
  <el-dialog :title="title" :visible.sync="dialogFormVisible" width="500px" @close="close">
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="名称" prop="name">
        <el-input v-model.trim="form.name" autocomplete="off" />
      </el-form-item>
      <el-form-item label="描述" prop="devicesInfo">
        <el-input v-model.trim="form.devicesInfo" autocomplete="off" />
      </el-form-item>
      <el-form-item label="标签" prop="tag">
        <el-input v-model.trim="form.tag" autocomplete="off" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import api_device from '@/api/device/API_device.js'

  export default {
    name: 'TableEdit',
    data() {
      return {
        method: 'create',
        form: {
          title: '',
          author: '',
        },
        rules: {
          title: [{ required: true, trigger: 'blur', message: '请输入标题' }],
          author: [{ required: true, trigger: 'blur', message: '请输入作者' }],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      showEdit(row) {
        if (!row) {
          this.title = '添加'
          this.method = 'create'
        } else {
          this.title = '编辑'
          this.method = 'update'
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
        this.$emit('fetch-data')
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if (this.method == 'create') {
              this.create()
            } else {
              this.update()
            }
          } else {
            return false
          }
        })
      },
      async create() {
        console.info('新增数据', this.form)
        // const { msg } = await doEdit(this.form)
        // this.$baseMessage(msg, 'success')
        // this.$refs['form'].resetFields()
        // this.dialogFormVisible = false
        // this.$emit('fetch-data')
        // this.form = this.$options.data().form
      },
      async update() {
        console.info('更新数据', this.form)
        let dataNew = {
          name: this.form.name,
          devicesInfo: this.form.devicesInfo,
          tag: this.form.tag,
        }
        let when = {
          sid: this.form.sid,
        }
        const { datas, resp_code, resp_msg } = await api_device.update(dataNew, when)
        if (resp_code < 0) {
          this.$baseMessage(resp_msg, 'error')
        } else {
          this.$baseMessage('更新成功', 'success')
          this.$refs['form'].resetFields()
          this.dialogFormVisible = false
          this.$emit('fetch-data')
          this.form = this.$options.data().form
        }
      },
    },
  }
</script>
